<?php
/**
 * Author:Albert Zhan(http://www.5lazy.cn)
 * Date: 2017/4/1 0001
 * Time: 11:54
 */
namespace Home\Controller;
class PublicController extends \Common\Controller\BaseController{

    public function test(){
        $this->display();
    }

    /**
     * 查询数据库字段描述
     */
    public function get_field_comment(){
        //查询数据库内的字段
        $table = $_POST['table'];
        $table_arr = explode(',',$table);
        $rows[$table_arr[0]]=M()->query("SHOW FULL FIELDS FROM `$table_arr[0]`");
        $result=array();
        $len = count($table_arr);
        if($len > 1){
            for($i=1;$i<$len;$i++){
                $rows[$table_arr[$i]]= M()->query("SHOW FULL FIELDS FROM `$table_arr[$i]`");
            }
        }
        //做字段归类及别名
        $filed_arr = explode(',', $_POST['field']);
        foreach ($filed_arr as $k => $v) {
            list($table_name, $field_name) = explode('.', $v);
            $field_as_name=explode('as',$field_name);
            if(!empty($rows[$table_name])){
                foreach($rows[$table_name] as $key=>$value){
                    if(trim($field_as_name[0])==$value['field']){
                        if(isset($field_as_name[1])){
                            $value['field']=trim($field_as_name[1]);
                            $result[]=$value;
                        }
                        else{
                            $result[]=$value;
                        }
                    }
                }
            }
        }
        //新增自定义字段
        $add_field_arr=explode(',',$_POST['add_field']);
        if(!empty($add_field_arr[0])){
            foreach($add_field_arr as $k=>$v){
                $add_field_name=explode('@',$v);
                $result[]=array(
                    'field'=>$add_field_name[1],
                    'type'=>$add_field_name[0],
                    'comment'=>$add_field_name[2],
                );
            }
        }
        $this->data['rows'] = $result;
        $this->rtnResult(0,'success');
    }

}